<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
      <TreeTolls :treeNode='company' :is-root='true'  @addDepts="addDepts"></TreeTolls>
        <el-tree
          :data="depts"
          :props="defaultProps"
          @node-click="handleNodeClick"
          :default-expand-all='true'
        >
        <TreeTolls slot-scope="{ data }" :treeNode='data' @delDepts='getDepartments'  @addDepts="addDepts"  @editDepts="editDepts"></TreeTolls>
        </el-tree>
      </el-card>
    </div>
    <AddDept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments"></AddDept>
  </div>
</template>

<script>
import TreeTolls from './components/tree-tolls.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData }from'@/utils'
import AddDept from '@/views/departments/components/add-dept.vue'
export default {
  components:{TreeTolls,AddDept},
  data() {
    return {
      showDialog: false ,// 显示窗体
      depts: [
      ],
      defaultProps: {
        children: "children",
        label: "name",
      },
      company: { name: '', manager: '' },
      node:null
    };
  },
  created() {
    this.getDepartments() // 调用自身的方法
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: ''}
      this.depts = tranListToTreeData(result.depts,'') // 需要将其转化成树形结构
      console.log(result)
    },
    handleNodeClick(data) {
      console.log(data);
    },
    addDepts(node) {
      this.showDialog = true // 显示弹层
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.node = node
    },
    editDepts(node) {
      // 首先打开弹层
      this.showDialog = true
      this.node = node // 赋值操作的节点
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>